package command;

import parser.ChessContentHandlerConstants;
import parser.ParserConstants;


public class PromoteCommand implements Command {

	private chess.Piece originalPiece,newPiece;
	private String newType;
	private chess.Tile location;
	private Command secondaryCommand;
	public PromoteCommand(String newType,chess.Tile where){
		originalPiece=where.getPieceHere();
		//System.out.println(originalPiece.toString());
		location=where;
		this.newType=newType;
		secondaryCommand=new NoSecondaryCommand();
		
	}
	
	@Override
	public void execute() {
		originalPiece.getPlayer().getMyPieces().remove(originalPiece); //Take the original piece
																		//Away from the player
		if (newType.equals("Knight")){
			newPiece=new chess.KnightPiece(originalPiece.getPlayer(),location);
		}
		else if (newType.equals("Rook")){
			newPiece=new chess.RookPiece(originalPiece.getPlayer(),location);
		}
		else if (newType.equals("Bishop")){
			newPiece=new chess.BishopPiece(originalPiece.getPlayer(),location);
		}
		else if (newType.equals("Queen")){
			newPiece=new chess.QueenPiece(originalPiece.getPlayer(),location);
		}
		location.setPieceHere(newPiece); //Set the piece in the square
		secondaryCommand.execute();
	}

	public void addSecondaryCommand(Command secondaryCommand) {
		if (this.secondaryCommand instanceof NoSecondaryCommand){
			this.secondaryCommand = secondaryCommand;
		}
		else{
			this.secondaryCommand.addSecondaryCommand(secondaryCommand);
		}
	}

	@Override
	public void undo() {
		secondaryCommand.undo();
		location.setPieceHere(originalPiece);
		newPiece.getPlayer().getMyPieces().remove(newPiece);
		originalPiece.getPlayer().getMyPieces().add(originalPiece);

	}
	
	public boolean hadMovedPreviously(){
		return true;
	}
	/**
	 * This method will return a string representation of the command used for importing and
	 * exporting
	 * @return Returns a string representing the object
	 */
	public String exportCode(){
		return "Promote:" + location.toString() + "to" + newType;
	}
	/**
	 * This method will return a string representation of the command used for saving and
	 * loading
	 * @return Returns a string representing the object
	 */
	@Override
	public String xmlCode() {
		String code = "<" + ParserConstants.ELEMENT_PROMOTE + ">\n";
		
		code += "\t" + location.XMLPointTag(ChessContentHandlerConstants.POINT_1).replaceAll("\n", "\n\t");
		code += "\n";
		code += "\t" + XMLPieceTag().replaceAll("\n", "\n\t");
		code += "\n";
		code += "</" + ParserConstants.ELEMENT_PROMOTE + ">";
		code += secondaryCommand.xmlCode();
		return code;
	}
	/**
	 * This method is a helper method to turn the newPiece object into an XML string
	 * @return Returns a string representing the piece
	 */
	private String XMLPieceTag(){
		String s = "<" + ChessContentHandlerConstants.ELEMENT_PIECE + " ";
		
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + newType + "'>";
		
		s += "</" + ChessContentHandlerConstants.ELEMENT_PIECE + ">";
		return s;
	}
	@Override
	public Command getSecondaryCommand() {
		return secondaryCommand;
	}
	
}
